package com.esint.kafka.roles;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @description: 生产者类
 * @author: 谷明
 * @createDate: 2021/8/14 14:04
 */
@Component
@Slf4j
public class ProducerOne {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    private Gson gson = new GsonBuilder().create();

    //发送消息方法
    public void send() {
        Message message = new Message();
        message.setId(UUID.randomUUID().toString());
        message.setMsg(UUID.randomUUID().toString());
        message.setSendTime(LocalDateTime.now());
        log.info("+++++++++++++++++++++ Produceer Says  message = {}", gson.toJson(message));
        kafkaTemplate.send("boot_test", gson.toJson(message));
    }

}
